package handler

import (
	"github.com/gin-gonic/gin"
	"video-proj/inits"
	"video-proj/internal/dto"
	"video-proj/internal/model"
	"video-proj/response"
)

func Login(c *gin.Context) {

	var user model.User
	err := c.ShouldBind(&user)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	var dbUser model.User
	err = dto.FindTel(&dbUser, user.Tel)
	if err != nil {
		newUser := model.User{

			Tel:      user.Tel,
			Password: user.Password,
		}
		_, err = inits.Rdb.Get(inits.Ctx, "sendSms"+user.Tel).Result()
		if err != nil {
			response.Error(c, "短信验证码错误")
			return
		}
		err = dto.Create(&newUser)
		if err != nil {
			response.Error(c, "注册失败")
			return
		} else {
			response.Success(c, "注册成功", newUser.ID)
			return

		}
	}

	if dbUser.Password != user.Password {
		response.Error(c, "密码错误")
		return
	}
	response.Success(c, "登录成功", dbUser.ID)
}
